package cn.itcast.controller.cargo;

import cn.itcast.base.BaseController;
import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.ContractExample;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.utils.commons.UtilFuns;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping(value = "/cargo/contract")
public class ContractController extends BaseController {

    @Reference
    private ContractService contractService;

    @RequestMapping(value = "/list", name = "合同的分页数据查询")
    public String findAll(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "4") int pageSize) {

        ContractExample example = new ContractExample();
        // 根据合同创建时间排序
        example.setOrderByClause("create_time desc");

        //设置条件
        ContractExample.Criteria criteria = example.createCriteria();
        criteria.andCompanyIdEqualTo(companyId);
        //细粒度的权限控制（根据当前用户的不同级别 展示不同的数据）
        if (user.getDegree() == 4) {//4-普通员工
            criteria.andCreateByEqualTo(user.getId());
        } else if (user.getDegree() == 3) {//管理本部门 (经理：部门id)
            criteria.andCreateDeptEqualTo(user.getDeptId());
        } else if (user.getDegree() == 2) {//2-管理所有下属部门和人员
            criteria.andCreateDeptLike(user.getDeptId() + "%");
        }	//1-企业管理员

        PageInfo page = contractService.findAll(example, pageNum, pageSize);
        request.setAttribute("page", page);
        return "cargo/contract/contract-list";
    }

    @RequestMapping(value = "/toAdd", name = "合同的新增页面")
    public String toAdd() {
        return "cargo/contract/contract-add";
    }


    @RequestMapping(value = "/edit", name = "合同的新增")
    public String edit(Contract contract) {
        // 设置企业标识
        contract.setCompanyId(companyId);
        // 设置企业名称
        contract.setCompanyName(companyName);

        if (UtilFuns.isEmpty(contract.getId())) {
            // 保存
            // 保存创建人和创建部门
            contract.setCreateBy(user.getId());
            contract.setCreateDept(user.getDeptId());
            contractService.save(contract);
        } else {
            contractService.update(contract);
        }
        // 到最新页面展示最新数据
        return "redirect: /cargo/contract/list.do";
    }


    @RequestMapping(value = "/toUpdate", name = "合同编辑页面")
    public String toUpdate(String id) {
        // 获取要修改的合同
        Contract contract = contractService.findById(id);
        request.setAttribute("contract", contract);

        return "cargo/contract/contract-update";
    }


    @RequestMapping(value = "/delete", name = "合同的删除")
    public String del(String id) {
        contractService.delete(id);
        // 到最新页面展示最新数据
        return "redirect: /cargo/contract/list.do";
    }


    @RequestMapping(value = "/toView", name = "合同查看页面")
    public String toView(String id) {
        // 获取要修改的合同
        Contract contract = contractService.findById(id);
        request.setAttribute("contract", contract);

        return "cargo/contract/contract-view";
    }


    @RequestMapping(value = "/submit", name = "合同的提交")
    public String submit(String id) {

        // 根据id获取合同
        Contract contract = contractService.findById(id);
        // 修改这个合同的state状态
        contract.setState(1);
        // 修改合同
        contractService.update(contract);
        // 到最新页面展示最新数据
        return "redirect: /cargo/contract/list.do";
    }

    @RequestMapping(value = "/cancel", name = "合同的取消")
    public String cancel(String id) {

        // 根据id获取合同
        Contract contract = contractService.findById(id);
        // 修改这个合同的state状态
        contract.setState(0);
        // 修改合同
        contractService.update(contract);
        // 到最新页面展示最新数据
        return "redirect: /cargo/contract/list.do";
    }


}
